module MC.Call {
	export class MsgList {
		public constructor() {
		}
		private uid: string = '';
		public getlist() {
			MC.Call.Jiguang((uid: string, obj: any) => {
				this.uid = uid;
				this.onMsgReceive();
			});
			/* MC.api('getmsglist', {}, (json: any) => { 				
				this.upList(json);
			}); */
		}
		private onMsgReceive() {
			JIM.getConversation().onSuccess((data: any) => {
				$('.load').hide();
				console.log(data);
				//	for(let i=0;i<data.conversations.length;i++){
				this.upList(data.conversations);
				///	}
			});
		}

		private upList(list: any) {
			for (let i = list.length - 1; i >= 0; i--) {
				let li = list[i];
				if (li.unread_msg_count == 0) {
					li.unread_msg_count = '';
				} else {
					li.unread_msg_count = `<span class="count">` + li.unread_msg_count + `</span>`;
				}
				let html = `<a href="jobWin.html?uid=` + MC.API.jiguangIdToSytem(li.username) + `" 
					class="aui-six-list-item">
						<div class="aui-flex b-line">
							<div class="aui-flex-box">
								
								<h3>`+ li.unread_msg_count + ` ` + li.nickName + `</h3>
								
								<div class="clearfix">
								<p>`+ MC.API.timeTostr(li.mtime) + `</p>
									</div>
								</div>
							<div class="qujiaoliuDiv" ><p>去交流</p>
						</div>
					</div>
				</a>`;
				$('#msglist').append(html);
			}
		}

	}


	export function getmsgUnreadCount() {
		if (!$('#toMessagePage')[0]) {
			return;
		}
		let count: any = localStorage.getItem('getmsgUnreadCount');
		if (count && count > 0) {
			let msgon = `<i class="msgOn"></i>`;
			$('#toMessagePage').append(msgon);
			$('#business').text(count);
		}
		
		let uid = '';
		MC.Call.Jiguang((_uid: string, obj: any) => {
			uid = _uid;
			JIM.getConversation().onSuccess((data: any) => {
				console.log(data.conversations);
				let list = data.conversations;
				let count = 0;
				for (let i = 0; i < list.length; i++) {
					let one = list[i];
					count += one.unread_msg_count;
				}
				localStorage.setItem('getmsgUnreadCount', count);
				$('#business').text(count);
				if (count > 0) {
					if (!$('#toMessagePage .msgOn')[0]) {
						let msgon = `<i class="msgOn"></i>`;
						$('#toMessagePage').append(msgon);
					}
				} else {
					$('#toMessagePage .msgOn').hide();
				}


			});
		});

		/*		
		MC.api('getmsgUnreadCount', {}, (json: any) => {
			$('#business').text(json);
			localStorage.setItem('getmsgUnreadCount', json);
			if (json > 0) {
				if (!$('#toMessagePage .msgOn')[0]) {
					let msgon = `<i class="msgOn"></i>`;
					$('#toMessagePage').append(msgon);
				}
			} else {
				$('#toMessagePage .msgOn').hide();
			}
		});*/


	}
	$(() => {
		getmsgUnreadCount();
	});
}